﻿using System;
using System.Collections.Generic;
using System.Linq;
using context;
using entities;
using System.Transactions;

namespace DataAccessLibrary.DAL
{
    public class CustomerDAO
    {
        private BlueMDataContext _ctx;
        private const string Success = "SUCCESS";
        private const String Fail = "FAIL: ";

        public CustomerDAO()
        {
            _ctx = new BlueMDataContext();
        }

        public List<Customer> ReadAllCustomer()
        {
            try
            {
                var query = _ctx.Customers;
                return query.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertCustomer(Customer customer)
        {
            try
            {
                using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 0, 30)))
                {

                    string id = customer.CustomerID;
                    int pfx = 1;
                    if (!CheckCustomerId(id))
                    {
                        while (!CheckCustomerId(id + pfx))
                        {
                            pfx++;
                        }
                        id = id + pfx;
                    }
                    customer.CustomerID = id;
                    _ctx.Customers.InsertOnSubmit(customer);
                    _ctx.SubmitChanges();
                    ts.Complete();
                    return Success;
                }
            }
            catch (Exception ex)
            {
                _ctx = new BlueMDataContext();
                return Fail + ex.Message;
            }
        }

        public bool CheckCustomerEmail(string email)
        {
            IQueryable<Customer> query = _ctx.Customers;
            query = query.Where(p => p.CustomerEmail == email);
            return query.FirstOrDefault() == null;
        }

        public bool CheckCustomerId(string id)
        {
            IQueryable<Customer> query = _ctx.Customers;
            query = query.Where(p => p.CustomerEmail == id);
            return query.FirstOrDefault() == null;
        }

        public string UpdateCustomer(Customer customer)
        {
            try
            {
                using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 0, 30)))
                {
                    var id = customer.CustomerID;
                    var c = FindCustomerByID(id);

                    c.CustomerName = customer.CustomerName;
                    c.CustomerSex = customer.CustomerSex;
                    if (customer.CustomerPhone != "")
                        c.CustomerPhone = customer.CustomerPhone;
                    if (customer.CustomerEmail != "")
                    {
                        if (CheckCustomerEmail(customer.CustomerEmail))
                        {
                            c.CustomerEmail = customer.CustomerEmail;
                        }
                        return Fail + "Customer Email exit";
                    }
                        
                    c.ModDay = DateTime.Today;
                    _ctx.SubmitChanges();
                    ts.Complete();
                    return Success;
                }

            }
            catch (Exception ex)
            {
                _ctx = new BlueMDataContext();
                return Fail + ex.Message;
            }
        }

        public List<Customer> FindCustomerByName(string CustomerName)
        {
            IQueryable<Customer> query = _ctx.Customers;
            query = query.Where(p => p.CustomerName.Contains(CustomerName));
            return query.ToList();
        }

        public Customer FindCustomerByID(string pIdentityNumber)
        {
            IQueryable<Customer> query = _ctx.Customers;
            query = query.Where(p => p.CustomerID == pIdentityNumber);
            return query.FirstOrDefault();
        }

        public Customer FindExtUserByEmail(string pEmail)
        {
            IQueryable<Customer> query = _ctx.Customers;
            query = query.Where(p => p.CustomerEmail == pEmail);
            return query.FirstOrDefault();
        }

        public Customer FindExtUserByAccName(string pUN)
        {
            IQueryable<Customer> query = _ctx.Customers;
            query = query.Where(p => p.CustomerAccount.Contains(pUN));
            return query.FirstOrDefault();
        }

        public string UpdateUserPW(string pUN, string pPW)
        {
            try
            {
                Customer c = FindExtUserByAccName(pUN);
                c.CustomerPassword = pPW;
                _ctx.SubmitChanges();
                return Success;
            }
            catch (Exception ex)
            {
                return Fail + ex.Message;
            }

        }

        public int CustomerCount()
        {
            return _ctx.Customers.Count();
        }

        public string CheckUNAvailible(string pUserName)
        {
            int i = 0;
            string result = pUserName;
            while (!CheckUNExist(pUserName))
            {
                i++;
                if (i != 1)
                    pUserName = pUserName.Substring(0, pUserName.Length - 1);

                pUserName += i.ToString();
                result = pUserName;
            }
            return result;
        }

        public bool CheckUNExist(string pUserName)
        {
            IQueryable<Customer> query = _ctx.Customers;
            query = query.Where(p => p.CustomerAccount == pUserName);
            return !query.Any();
        }
    }
}
